﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrNhapKhoXeKien : DevComponents.DotNetBar.Office2007Form
    {
        public FrNhapKhoXeKien()
        {
            InitializeComponent();
        }
        private string ChungTu1, NguonGoc1, TKHaiQuan1, LoaiGo1, LyDoNhap1, NhapTu1, child= " - ",PhanLoai1,FCS1;
        public string sql_select = @"select * from dbo.Serial_NKGo";
        private void combo_load()
        {
            Data dta=new Data();
            FormProcess Frp = new FormProcess();
            string[] arr = new string[] { "AD", "KD" };
            for (int i = 0; i < arr.Length; i++)
            {
                cb_PhanLoai.Items.Add(arr[i].ToString());
                //.Items.Add(arr[i].ToString());
            }
            string[] arr_FCS = new string[] { "FSC", "CSP","SMP" };
            for (int j = 0; j < arr_FCS.Length; j++)
            {
                cb_FCS.Items.Add(arr_FCS[j].ToString());
                //.Items.Add(arr[i].ToString());
            }
   
            string sql_LoaiGo = @"select MaLoai from dbo.LoaiGo";
            SqlConnection Conn = dta.Connect();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql_LoaiGo;
            cmd.Connection = Conn;
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                cb_LoaiGo.Items.Add(rd["MaLoai"].ToString());
                
                //.Items.Add(rd[column1].ToString());      
            }
            string sql_NCC = @"select MaNCC,TenNCC from dbo.NhaCungCap";
            Frp.cb1(cb_NhapTu, sql_NCC, 0, 1);
            sql_NCC = @"select MaLyDo,LyDo from dbo.LyDo where MaLyDo like 'N%'";
            Frp.cb1(cb_LyDoNhap,sql_NCC, 1,0);
            Conn.Close();
            cmd.Dispose();
            rd.Close();
            
        }

        private void FrNhapKhoXeKien_Load(object sender, EventArgs e)
        {
            txt_ChungTu.ReadOnly = true;
            combo_load();
            linkLabel1.Visible = false;
            
        }
        private void Save()
        {
            Process prs = new Process();
            //ChungTu1 = txt_ChungTu.Text.Trim();
            NguonGoc1 = txt_NguonGoc.Text.Trim();
            TKHaiQuan1 = txt_TKHaiQuan.Text.Trim();
            LoaiGo1 = cb_LoaiGo.Text.Trim();
            PhanLoai1 = cb_PhanLoai.Text.Trim();
            LyDoNhap1 = prs.substring_right(cb_LyDoNhap.Text, child);
            FCS1 = cb_FCS.Text.Trim();
            try
            {
                if(cb_PhanLoai.Text=="AD"||cb_PhanLoai.Text=="KD")
                {
                    NhapTu1 = prs.substring_left(cb_NhapTu.Text, child);
                    SqlConnection Conn = prs.Connect();
                    Conn.Open();
                    string getmonth = @"select month(getdate())-month(NgayNhap)+year(getdate())-year(NgayNhap) from dbo.Serial_NKGo where Serial_NK=(select max(Serial_NK) from dbo.Serial_NKGo)";
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = getmonth;
                    cmd.Connection = Conn;
                    int month = int.Parse(cmd.ExecuteScalar().ToString());
                    //MessageBox.Show(month.ToString());
                    string date = string.Format("{0:MM/yy}", DateTime.Now);
                    string sql_insert = null;
                    //MessageBox.Show(date);
                    if (month > 0)
                    {
                        ChungTu1 = "1" + child + date + "/NKNCC";
                        sql_insert = @"insert into dbo.Serial_NKGo(ChungTu,NguonGoc,TKHaiQuan,HDNgay,LoaiGo,LyDoNhap,ADorKD,FCS,NhapTu,NgayHD) values(N'";
                        sql_insert += ChungTu1 + "',N'" + NguonGoc1 + "',N'" + TKHaiQuan1 + "','" + dtp_HDNgay.Value + "',N'" + cb_LoaiGo.Text.Trim() + "',N'" + LyDoNhap1 + "',N'" + PhanLoai1 + "',N'" + FCS1 + "',N'";
                        sql_insert += NhapTu1 + "','" + dtp_Ngay.Value + "')";
                    }
                    if (month <= 0)
                    {
                        string sql_ChungTu = @"select ChungTu From dbo.Serial_NKGo where Serial_NK=(select max(Serial_NK) from dbo.Serial_NKGo)";
                        cmd.CommandText = sql_ChungTu;
                        int ChungTu_input = int.Parse(prs.substring_left(cmd.ExecuteScalar().ToString(), child));
                        ChungTu1 = (ChungTu_input + 1).ToString() + child + date + "/NCC";
                        sql_insert = @"insert into dbo.Serial_NKGo(ChungTu,NguonGoc,TKHaiQuan,HDNgay,LoaiGo,LyDoNhap,ADorKD,FCS,NhapTu,NgayHD) values(N'";
                        sql_insert += ChungTu1 + "',N'" + NguonGoc1 + "',N'" + TKHaiQuan1 + "','" + dtp_HDNgay.Value + "',N'" + cb_LoaiGo.Text.Trim() + "',N'" + LyDoNhap1 + "',N'" + PhanLoai1 + "',N'" + FCS1 + "',N'";
                        sql_insert += NhapTu1 + "','" + dtp_Ngay.Value + "')";
                    }
                    //SqlCommand cmd = new SqlCommand(sql_insert, Conn);
                    cmd.CommandText = sql_insert;
                    //MessageBox.Show(sql_insert);
                    cmd.ExecuteNonQuery();
                    string sql_Serial = @"select Serial_NK from dbo.Serial_NKGo where Serial_NK=(select max(Serial_NK) from dbo.Serial_NKGo)";
                    cmd.CommandText = sql_Serial;
                    lb_Serial.Text = cmd.ExecuteScalar().ToString();
                    MessageBox.Show("Dữ liệu đã được thêm thành công");
                    prs.reset_txt(groupBox1);
                    bt_save.Enabled = false;
                    //MessageBox.Show(sql_insert);
                    txt_ChungTu.ReadOnly = true;
                    Conn.Close();
                    cmd.Dispose();
                    linkLabel1.Visible = true;
                    FrNhapChiTiet FrNCT = new FrNhapChiTiet();
                    FrNCT.Show();
                    FrNCT.ChungTu(ChungTu1);
                    FrNCT.Get_Serial(lb_Serial.Text);
                    FrNCT.User(lb_User.Text);
                    FrNCT.Display_XK();
                }
                else
                {
                    MessageBox.Show("Vui lòng kiểm tra lại thông tin nhập");
                    cb_PhanLoai.Focus();
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { 
                    MessageBox.Show("Nhập sai thông tin trong Combobox");
                    cb_LoaiGo.Focus();
                }
                else
                    MessageBox.Show("Đã xảy ra lỗi khi thêm dữ liệu, Vui Lòng kiểm tra lại"+ ex.Message);
            }
            
        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(dtp_HDNgay.Value.ToString());
            Data dta = new Data();
            //txt_ChungTu.ReadOnly = false;
            dta.reset_txt(groupBox1);
            dta.reset_cb(groupBox1);
            //txt_ChungTu.Focus();
            bt_save.Enabled = true;
            dtp_Ngay.Focus();
            txt_ChungTu.ReadOnly = true;

        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            Save();
            txt_ChungTu.ReadOnly = true;
        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            try
            {
                bt_save.Enabled = false;
                Data dta=new Data();
                txt_ChungTu.ReadOnly = true;
                int i = int.Parse(lb_Serial.Text);
                string sql_select = @"select * from dbo.Serial_NKGo where Serial_NK="+i;
                SqlConnection Conn = dta.Connect();
                Conn.Open();
                SqlCommand cmd = new SqlCommand(sql_select, Conn);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    txt_ChungTu.Text = rd["ChungTu"].ToString();
                    txt_NguonGoc.Text = rd["NguonGoc"].ToString();
                    txt_TKHaiQuan.Text = rd["TKHaiQuan"].ToString();
                    dtp_Ngay.Value = DateTime.Parse(rd["NgayHD"].ToString());
                    dtp_HDNgay.Value = DateTime.Parse(rd["HDNgay"].ToString());
                    cb_LoaiGo.Text = rd["LoaiGo"].ToString();
                    cb_NhapTu.Text = rd["NhapTu"].ToString();
                    cb_PhanLoai.Text = rd["ADorKD"].ToString();
                    cb_FCS.Text = rd["FCS"].ToString();
                    cb_LyDoNhap.Text = rd["LyDoNhap"].ToString();

                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_edit_Click(object sender, EventArgs e)
        {
            if (txt_ChungTu.Text!="")
            {
                Process prs = new Process();
                FormProcess Frp = new FormProcess();
                DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa Thông Tin của Chứng Từ " + txt_ChungTu.Text, "Thông Báo", MessageBoxButtons.OKCancel);
                if (d == DialogResult.OK)
                {
                    try
                    {
                        if (cb_PhanLoai.Text == "AD" || cb_PhanLoai.Text == "KD")
                        {
                            SqlConnection Conn = prs.Connect();
                            Conn.Open();
                            string sql_update = null;
                            ChungTu1 = txt_ChungTu.Text.Trim();
                            NguonGoc1 = txt_NguonGoc.Text.Trim();
                            TKHaiQuan1 = txt_TKHaiQuan.Text.Trim();
                            LoaiGo1 = cb_LoaiGo.Text.Trim();
                            PhanLoai1 = cb_PhanLoai.Text.Trim();
                            LyDoNhap1 = prs.substring_right(cb_LyDoNhap.Text, child);
                            if (LyDoNhap1 == "")
                            {
                                LyDoNhap1 = cb_LyDoNhap.Text;
                            }
                            FCS1 = cb_FCS.Text.Trim();
                            NhapTu1 = prs.substring_left(cb_NhapTu.Text, child);
                            if (NhapTu1 == "")
                                NhapTu1 = cb_NhapTu.Text;
                            int i = int.Parse(lb_Serial.Text);
                            sql_update = "update dbo.Serial_NKGo set ChungTu=N'" + ChungTu1 + "',NguonGoc=N'" + NguonGoc1 + "',TKHaiQuan=N'";
                            sql_update += TKHaiQuan1 + "',HDNgay='" + dtp_HDNgay.Value + "',LoaiGo=N'" + LoaiGo1 + "',LyDoNhap=N'";
                            sql_update += LyDoNhap1 + "',ADorKD=N'" + PhanLoai1 + "',FCS=N'" + FCS1 + "',NhapTu=N'" + NhapTu1 + "',NgayHD='";
                            sql_update += dtp_Ngay.Value + "' where Serial_NK=" + i;
                            //MessageBox.Show(sql_update);
                            SqlCommand cmd = new SqlCommand(sql_update, Conn);
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Thông tin chỉnh sửa thành công");
                            prs.reset_txt(groupBox1);
                            Conn.Close();
                            cmd.Dispose();
                            cb_NhapTu.Text = "";
                            cb_PhanLoai.Text = "";
                            cb_FCS.Text = "";
                            cb_LyDoNhap.Text = "";
                            cb_LoaiGo.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("Kiểm tra lại thông tin nhập");
                            cb_PhanLoai.Focus();
                            
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Đã có lỗi xảy ra khi chỉnh sửa dữ liệu, Vui lòng kiểm tra lại");
                    }
                    bt_save.Enabled = false;
                }
            }
        }
        public void User(string User)
        {
            lb_User.Text = User;
        }

        private void FrNhapKhoXeKien_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F1)
                bt_insert_Click(null, null);
            if (ex.KeyCode == Keys.F11)
                Save();
        }
        private void Fr_report()
        {
            Data dta = new Data();
            int i = int.Parse(lb_Serial.Text);
            string sql_select = @"select Seri.ChungTu,Seri.NguonGoc,Seri.TKHaiQuan,Seri.HDNgay,Seri.ADorKD,Seri.LoaiGo,Seri.FCS,Seri.NhapTu,Nhap.ChieuDay as ChieuRong,Nhap.ChieuRong as ChieuDai,Nhap.ChieuDai as ChieuCao,Nhap.SoKhoi,Nhap.SoLuong,Nhap.GhiChu,NhaCungCap.TenNCC,Nhap.GhiChu as DVT,Seri.NgayHD
                                  from dbo.Serial_NKGo as Seri inner join dbo.NhapKhoGo as Nhap on Seri.Serial_NK=Nhap.Serial_NK inner join dbo.NhaCungCap on Seri.NhapTu=NhaCungCap.MaNCC
									where Seri.Serial_NK=" + i;
            DataSet ds = dta.dts(sql_select);
            Fr_Report FrR = new Fr_Report();
            FrR.report_NKXK(ds);
            FrR.Show();
            ds.Dispose();
        }

        private void bt_report_Click(object sender, EventArgs e)
        {
            Fr_report();
        }

    }
}
